package com.myclass.dynamicdatasource.controller;

import com.myclass.dynamicdatasource.entity.User;
import com.myclass.dynamicdatasource.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * Desc:
 *
 * @author wujw
 * @email jiwenwu@outlook.com
 * @date 2018-12-12
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getType")
    public List<User> getUser(String type){
        if ("one".equals(type)){
            return userService.getUserOne();
        }
        if ("two".equals(type)){
            return userService.getUserTwo();
        }
        return null;
    }
    @RequestMapping("/insertTypeOne")
    public void insert(){
        int a = 1;
        User user = new User();
        user.setUserName(UUID.randomUUID().toString());
        for (int i = 0; i < 10000; i++) {
            a ++;
            user.setId(a);
            userService.insertTypeOne(user);
        }
    }
    @RequestMapping("/insertTypeTwo")
    public void inserttwo(){
        int a = 1;
        User user = new User();
        user.setUserName(UUID.randomUUID().toString());
        for (int i = 0; i < 10000; i++) {
            a ++ ;
            user.setId(a);
            userService.insertTypeTwo(user);
        }
    }
    @RequestMapping("/insertOneAndTwo")
    public void insertOneAndTwo(){
        User user = new User();
        user.setId(288);
        user.setUserName("测试事务");
        this.userService.insertOneAndTwo(user);
    }

    @RequestMapping("/getOneAndTwo")
    public List<User> getOneAndTwo(){
      return this.userService.getOneAndTwo();
    }
}
